unit fDictSelect;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, FIBQuery, pFIBQuery,
  uDM, ActnList;

type
  TfrmSelectDict = class(TForm)
    lstDict: TListBox;
    qryDict: TpFIBQuery;
    btnSelect: TButton;
    ActionList1: TActionList;
    actSelect: TAction;
    btnAdd: TButton;
    procedure actSelectUpdate(Sender: TObject);
    procedure actSelectExecute(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
  private
    { Private declarations }
    FDictTable: string;
    procedure FillCard;
  public
    { Public declarations }
    class function Select(DictTable: string; var ID: Integer): Boolean; overload;
    class function Select(DictTable: string): Integer; overload;
  end;

//var
//  frmSelectDict: TfrmSelectDict;

implementation

uses fInputStr;

{$R *.dfm}

{ TfrmDictSelect }

procedure TfrmSelectDict.FillCard;
var
  qry: TpFIBQuery;
begin
  lstDict.Items.Clear;
  qry := dmData.GetReadQry;
  try
    qry.SQL.Text := 'Select * from ' + FDictTable;
    qry.ExecQuery;
    while not qry.Eof do
    with qry do
      begin
        lstDict.Items.AddObject(FieldByName('AName').AsString, TObject(FieldByName('ID').AsInteger));
        qry.Next;
      end;
  finally
    qry.Free;
  end;
end;

class function TfrmSelectDict.Select(DictTable: string; var ID: Integer): Boolean;
var
  frm: TfrmSelectDict;
begin
  frm := TfrmSelectDict.Create(nil);
  with frm do
  try
    FDictTable := DictTable;
    FillCard;
    Result := (ShowModal = mrOk) and (lstDict.ItemIndex <> -1);
    if Result then
      ID := Integer(lstDict.Items.Objects[lstDict.ItemIndex]);
  finally
    frm.Free;
  end;
end;


class function TfrmSelectDict.Select(DictTable: string): Integer;
var
  AID: Integer;
begin
  if Select(DictTable, AID) then
    Result := AID
  else
    Result := 0;
end;

procedure TfrmSelectDict.actSelectUpdate(Sender: TObject);
begin
  actSelect.Enabled := lstDict.ItemIndex <> -1;
end;

procedure TfrmSelectDict.actSelectExecute(Sender: TObject);
begin
  if lstDict.ItemIndex <> -1 then
    ModalResult := mrOk;
end;

procedure TfrmSelectDict.btnAddClick(Sender: TObject);
var
  Str: string;
begin
  if TfrmInputStr.getStr('', Str) then
  begin
    dmData.DictCard_Add(Str, FDictTable);
    FillCard;
  end;
end;

end.
